package com.webside.front.filter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.xbean.spring.context.XmlWebApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.webside.mongo.model.SpreadBrowerRecords;

public class SourceDistinguishFilter extends HttpServlet implements Filter{
	@Autowired   
	private MongoTemplate mongoTemplate;
	/**
	 * 
	 */
	private static final long serialVersionUID = 5150505687417209319L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)arg0;     
        HttpServletResponse response  =(HttpServletResponse) arg1;      
        HttpSession session = req.getSession(true);
		// TODO Auto-generated method stub
        //
        String url="";
		 url = req.getScheme() + "://" + req.getServerName() +":"+req.getServerPort()+ req.getRequestURI();
		if (req.getQueryString() != null) {
			url += "?" + req.getQueryString();
		}
		String tag = (String) req.getParameter("spread_source");
		if(tag == null){
			arg2.doFilter(arg0, arg1);     
            return;
		}
		
		// 调用java自带的uuid生成唯一标示id
		UUID uuid = UUID.randomUUID();
		// 去掉不需要的字符
		String id = uuid.toString().replaceAll("\\-", "");
		// 为实体对象加入标示id
		SpreadBrowerRecords spreadBrowerRecords = new SpreadBrowerRecords();
		spreadBrowerRecords.setId(id);
		spreadBrowerRecords.setSpreadSupportCode(tag);
		spreadBrowerRecords.setBrowerUrl(url);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		spreadBrowerRecords.setBrowerTime(nowTime);
		mongoTemplate.insert(spreadBrowerRecords, "spread_brower_records");
		/*System.out.println(url);
		String [] s=url.split("spread_source");
		url=s[0];*/
		arg2.doFilter(arg0, arg1);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
		mongoTemplate = (MongoTemplate) wac.getBean("mongoTemplate");
	}

}
